package gang.com.mobelhelper.ui.activity;


import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import gang.com.mobelhelper.R;
import gang.com.mobelhelper.common.util.DensityUtil;
import gang.com.mobelhelper.di.component.AppCompent;

/**
 * 获取View的缓存图像显示
 */
public class AppDetailActivity extends BaseActivity {


    @BindView(R.id.viewcache)
    FrameLayout viewcache;
    @BindView(R.id.lin_appdetail)
    LinearLayout linAppdetail;

    @Override
    public int setLayout() {
        return R.layout.activity_appdetial;
    }

    @Override
    public void setupActivityComponent(AppCompent appCompent) {

    }

    @Override
    public void init() {
        //在这里拿到view
        View view = mApplication.getmView();
        Bitmap bitmap = getViewImageCache(view);
        if (bitmap != null) ;
        viewcache.setBackgroundDrawable(new BitmapDrawable(bitmap));
        //viewcache.setImageBitmap(bitmap);
        /**
         * 方法2直接在线性布局中添加一个view，但是报错
         */
        //linAppdetail.addView(view);

        /**
         * 确定屏幕的位置需要left和top
         */
        //首先获取顶上的view的位置
        int[] location = new int[2];
        view.getLocationOnScreen(location);
        int left = location[0];
        int top = location[1];

        ViewGroup.MarginLayoutParams marginLayoutParams = new ViewGroup.MarginLayoutParams(viewcache.getLayoutParams());
        marginLayoutParams.topMargin = top - DensityUtil.getStatusBarH(this);
        marginLayoutParams.leftMargin = left;
        marginLayoutParams.width = view.getWidth();
        marginLayoutParams.height = view.getHeight();
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(marginLayoutParams);
        viewcache.setLayoutParams(params);
        open();
    }

    private Bitmap getViewImageCache(View view) {
        //首先把view打开
        view.setDrawingCacheEnabled(true);
        //view绘制缓存
        view.buildDrawingCache();
        //拿到缓存
        Bitmap bitmap = view.getDrawingCache();
        if (bitmap == null) {
            return null;
        }
        bitmap = Bitmap.createBitmap(bitmap);
        //销毁掉缓存
        view.destroyDrawingCache();
        return bitmap;
    }

    //打开时候的动画
    private void open() {
        int h = DensityUtil.getScreenH(this);

        ObjectAnimator animator = ObjectAnimator.ofFloat(viewcache, "scaleY", 1, (float) h);
        //延时执行这个动画才可看到内容

        animator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
                viewcache.setBackgroundColor(getResources().getColor(R.color.white));
            }

            @Override
            public void onAnimationEnd(Animator animation) {

            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        animator.setStartDelay(500);
        animator.setDuration(10000);
        animator.start();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
